package com.oepm.web.controller;

import com.oepm.biz.StuDemoFormBiz;
import com.oepm.common.entity.Meta;
import com.oepm.common.entity.Result;
import com.oepm.common.utils.JWTUtil;
import com.oepm.dao.entity.QueryInfo;
import com.oepm.dao.entity.StuDemoFormPo;
import com.oepm.vo.DataVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@CrossOrigin//解决跨域
@RestController
@RequestMapping("/projectApply")
@Api(tags = "学生申请表")
public class StuDemoFormController {

    @Autowired
    private StuDemoFormBiz stuDemoFormBiz;
    @PostMapping("/insert")
    @ApiOperation("申报项目")
    public Result insertTeaForm(@RequestBody StuDemoFormPo stoPo,ServletRequest request){
        HttpServletRequest req= (HttpServletRequest) request;
        String token=req.getHeader("Authorization");
        stoPo.setStuid(JWTUtil.getUserId(token));
        int flag = stuDemoFormBiz.addStuDemoForm(stoPo);
        Result result;
        if (flag>0){
            result = new Result(Meta.CREATED);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }

    /**
     * 学生申请项目前查询是否已超过录入项目
     */
    @GetMapping("/verifyStu/{level}")
    public Result preVerify(@PathVariable String level,ServletRequest request){
        HttpServletRequest req= (HttpServletRequest) request;
        String token=req.getHeader("Authorization");
        //添加验证学生只能同时申请两个普通，或者一个普通一个重点
        List<StuDemoFormPo> list = stuDemoFormBiz.queryStuDemoForms(JWTUtil.getUserId(token));
        Result result = new Result(Meta.SUCCESS);;
        if (list.size()==1){
            for (StuDemoFormPo s : list){
                if (level.equals("重点") & s.getLevel().equals("重点") & (s.getTeacher_status().equals("待审核") | s.getTeacher_status().equals("已通过"))){
                    result = new Result(Meta.FAIL,"不能同时申请两个重点项目");
                }
            }
        }else if (list.size()>1){
            result = new Result(Meta.FAIL,"不能申请超过两个项目,若想申请请删除某个未审核/已驳回项目");
        }

        return result;
    }
    @PostMapping("/update")
    @ApiOperation("修改申报项目")
    public Result updateTeaForm(@RequestBody StuDemoFormPo stoPo,ServletRequest request){
        HttpServletRequest req= (HttpServletRequest) request;
        String token=req.getHeader("Authorization");
        //获取学号进行身份检测
        String userId = JWTUtil.getUserId(token);
        String stuid = stuDemoFormBiz.querystuidByid(stoPo.getId()+"");
        if(!stuid.equals(userId)){
            return new Result( Meta.UNAUTHORISE);
        }
        int flag = stuDemoFormBiz.updateStuDemoForm(stoPo);
        Result result;
        if (flag>0){
            result = new Result(Meta.SUCCESS);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }

    @GetMapping("/select")
    @ApiOperation("查询已申报项目")
    public Result selectTeaForm(ServletRequest request,QueryInfo queryInfo){
        HttpServletRequest req= (HttpServletRequest) request;
        String token=req.getHeader("Authorization");
        String userId = JWTUtil.getUserId(token);
        DataVo dataVo = stuDemoFormBiz.queryStuDemoForms(userId, queryInfo);
        Result result = new Result(Meta.SUCCESS);
        result.setData(dataVo);
        return result;
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除未审核，及审核未通过的题目")
    public Result deleteTeaForm(@PathVariable String id,ServletRequest request){
        HttpServletRequest req= (HttpServletRequest) request;
        String token=req.getHeader("Authorization");
        //获取学号进行身份检测
        String userId = JWTUtil.getUserId(token);
        String stuid = stuDemoFormBiz.querystuidByid(id);
        if(!stuid.equals(userId)){
            return new Result( Meta.UNAUTHORISE);
        }
        int flag = stuDemoFormBiz.deleteStuDemoForm(id);
        Result result;
        if (flag>0){
            result = new Result(Meta.SUCCESS);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }

}
